package com.topideal.supplychain.hades.master.service.impl;

import com.topideal.supplychain.framework.exception.BusinessException;
import com.topideal.supplychain.hades.master.mapper.CarrierMapper;
import com.topideal.supplychain.hades.master.model.Carrier;
import com.topideal.supplychain.hades.master.service.CarrierService;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
/**
 * 
 * TODO 
 *  
 * @author huanghonglin
 * @date 2017年9月26日 上午9:16:54
 */
@Service
public class CarrierServiceImpl implements CarrierService {
	private Logger logger = LoggerFactory.getLogger(CarrierServiceImpl.class);
	
	@Autowired
	private CarrierMapper carrierMapper;
	
	@Override
	public List<Carrier> findAll() {
		return carrierMapper.findAll();
	}

	@Override
	public List<Carrier> findBySelective(Carrier carrier) {
		return carrierMapper.findBySelective(carrier);
	}

	@Override
	@Transactional
	public int addCarrier(Carrier filter) {
		filter.setCreateTime(new Date());
		//filter.setCreateBy(Authentication.getUserName());
		return carrierMapper.insertCarrier(filter);
	}

	@Override
	public Carrier get(Long carrierId) {
		return carrierMapper.get(carrierId);
	}

	@Override
	@Transactional
	public int updateCarrier(Carrier filter) {
		filter.setUpdateTime(new Date());
		//filter.setUpdateBy(Authentication.getUserName());
		return carrierMapper.updateCarrier(filter);
	}

	/**
	 * 使数据不再显示在页面上。
	 */
	@Override
	@Transactional
	public int delete(String[] ids) {
		int row = 0;
		for(String id : ids) {
			row += carrierMapper.delete(id);
		}
		return row;
	}

	@Override
	@Transactional
	public int updateState(String[] ids) {
		//ids的最后一个值为state
		int row = 0;
		int state = 0;
		try {
		state = Integer.valueOf(ids[ids.length-1]);
		} catch (Exception e) {
			logger.error("state:"+state+"数据类型出错",e);
			throw new BusinessException("state:"+state+"数据类型出错",e);
		}
		for(int i=0;i<ids.length-1;i++) {
			Carrier carrier = carrierMapper.get(Long.valueOf(ids[i]));
			if(ObjectUtils.isEmpty(carrier)) {
				continue;
			}
			if(state==1) {
				//状态相同不需要更新。
				if(carrier.getState()==1) {
					continue;
				}
			} else {
				if(carrier.getState()==0) {
					continue;
				}
			}
			row += carrierMapper.updateState(state,ids[i]);
		}
		return row;
	}
	
	public List<Carrier> list(Carrier carrier) {
		return this.carrierMapper.list(carrier);
	}


	/**
	 * 根据id找name
	 */
	@Override
	public Long findIdByName(String name){
		return carrierMapper.findIdByName(name);
	}
	
	@Override
	public Carrier getByOutCarrierCode(String outCarrierCode) {
		return carrierMapper.selectByOutCarrierCode(outCarrierCode);
	}

	/**
	 * @author wanwei
	 * @TODO  通过上游系统物流企业编码查找
	 * @date: 2017年11月9日 上午10:05:19 
	 */
	@Override
	public Carrier findByKdniaoCode(String carrierCode) {
		return carrierMapper.findByKdniaoCode(carrierCode);
	}

	@Override
	public Carrier findByCode(String carrierCode) {
		return carrierMapper.findByCode(carrierCode);
	}

	/**
	 * 根据mapping表平台订单编号查找code,然后再carrier表中查找对应的值。
	 */
	@Override
	public Carrier findByOutCodeAndSubscriberCode(String subscriberCode,String carrierCode) {
		return carrierMapper.findByMappingOutCodeAndSubscriberCode(subscriberCode,carrierCode);
	}

	@Override
	public Carrier findByOutCarrierCode(String carrierCode) {
		return carrierMapper.findByOutCarrierCode(carrierCode);
	}

	@Override
	public Carrier findByKd100Code(String kd100Code) {
		// TODO Auto-generated method stub
		return carrierMapper.findByKd100Code(kd100Code);
	}
}
